# 导入模块
import time

from selenium import webdriver
from selenium.webdriver.common.by import By

# 准备数据
UserInfo = {
    'user': '1352769194@qq.com',
    'password': '*******'
}


class Email:
    # 初始化
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.get('https://mail.qq.com/')

        # 定位到qq登录
        self.driver.find_element(By.ID, 'qqLoginTab').click()
        # 切换到登录iframe
        self.driver.switch_to.frame('login_frame')

    # 登录
    def logon(self):
        # 定位账号输入框，输入数据
        self.driver.find_element(By.ID, 'u').send_keys(UserInfo['user'])
        # 定位到密码输入框，输入数据
        self.driver.find_element(By.ID, 'p').send_keys(UserInfo['password'])
        # 定位下次免登录按钮
        self.driver.find_element(By.CLASS_NAME, 'uncheck').click()
        # 定位到登录按钮，点击
        self.driver.find_element(By.ID, 'login_button').click()

    # 使用cookie
    def cookie(self):
        pass

    # 发电子邮件
    def send_emails(self):
        # 定位到写信，点击
        self.driver.find_element(By.PARTIAL_LINK_TEXT, '写信').click()

        # 切换到发邮件页面
        iframe = self.driver.find_element(By.XPATH, '//*[@id="mainFrame"]')
        self.driver.switch_to.frame(iframe)
        # 定位收件人输入框，输入数据
        self.driver.find_element(By.XPATH, '/html/body/form[2]/div[2]/div[3]/div[2]/table[2]/tbody/tr/td[2]/div[1]/div[2]/input').send_keys('selenium')
        # 定位主题输入框，输入数据
        self.driver.find_element(By.XPATH, '/html/body/form[2]/div[2]/div[3]/table[3]/tbody/tr[2]/td[2]/div/div/div/input').send_keys('selenium')
        # 切换到正文iframe
        iframe = self.driver.find_element(By.XPATH, '/html/body/form[2]/div[2]/div[7]/table/tbody/tr/td[2]/div/table/tbody/tr[2]/td/iframe')
        self.driver.switch_to.frame(iframe)
        # 定位正文输入框，输入数据
        self.driver.find_element(By.XPATH, '/html/body').send_keys('selenium')
        # 切换到发邮件iframe
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('mainFrame')
        # 定位发邮件按钮,点击
        self.driver.find_element(By.LINK_TEXT, '发送').click()

    def quit(self):
        # 退出
        self.driver.quit()


if __name__ == '__main__':
    lo = Email()
    time.sleep(5)
    lo.logon()
    time.sleep(5)
    lo.send_emails()
    time.sleep(5)
    lo.quit()
